package com.tange.ai.iot.client.gnssa.token

import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import androidx.appcompat.app.AppCompatActivity
import com.tange.ai.iot.client.gnssa.databinding.ActivityInternalAccountRegisterBinding
import com.tange.base.toolkit.Tips
import com.tange.base.ui.design.BasicDialogLoading
import com.tange.core.builtin.account.BuiltinAccount

/**
 * 使用探鸽账号体系，进行注册
 */
class InternalAccountRegisterActivity: AppCompatActivity() {

    private lateinit var binding: ActivityInternalAccountRegisterBinding

    private var loading: BasicDialogLoading? = null

    private var codeSent = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActivityInternalAccountRegisterBinding.inflate(layoutInflater).apply {

            setContentView(root)
            supportActionBar?.hide()

            submit.setOnClickListener {
                if (codeSent) {
                    registerAccount()
                } else {
                    sendCode()
                }
            }
        }

        window.apply {
            var uiFlags = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            uiFlags = uiFlags or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            statusBarColor = Color.TRANSPARENT
            decorView.systemUiVisibility = uiFlags
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            }
        }

        loading = BasicDialogLoading(this).apply {
            setMessage("登录中...")
        }

        com.tange.ai.iot.client.gnssa.utils.Utils.setStatusBar(this, Color.TRANSPARENT)
    }

    private fun sendCode() {
        val account = binding.username.text.toString().trim()

        if (TextUtils.isEmpty(account)) {
            Tips.show("请输入手机或者邮箱")
            return
        }

        BuiltinAccount.register().requireVerificationCode(this, account) {
            if (it.success) {
                codeSent = true
                binding.submit.setText("注册")
                Tips.show("验证码已发送，有效期为 ${it.data?.expiresInSeconds} s")
                binding.code.requestFocus()
            } else {
                Tips.show("验证码发送失败： ${it.message}")
            }
        }
    }

    private fun registerAccount() {
        val account = binding.username.text.toString().trim()

        if (TextUtils.isEmpty(account)) {
            Tips.show("请输入手机或者邮箱")
            return
        }

        val password = binding.password.text.toString().trim()

        if (TextUtils.isEmpty(password)) {
            Tips.show("请输入密码")
            return
        }

        val code = binding.code.text.toString().trim()

        if (TextUtils.isEmpty(code)) {
            Tips.show("请输入验证码")
            return
        }

        BuiltinAccount.register().create(account, code, password) {
            if (it.success) {
                Tips.show("注册成功，请登录~")
                finish()
            } else {
                Tips.show("注册失败： ${it.message}")
            }
        }
    }

    override fun onPause() {
        super.onPause()
        loading?.dismiss()
    }
}